package com.futao.region;

import lombok.*;
import org.apache.commons.lang3.StringUtils;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ft
 * @date 2021/5/13
 */
public class Demo {
    /**
     * 中国行政区划省市区
     *
     * @param args
     * @throws Throwable
     */
    public static void main(String[] args) throws Throwable {
        List<String> lines = Files.readAllLines(Paths.get("D:\\src\\my\\simple-demo-tool\\src\\main\\java\\com\\futao\\data.txt"));
        ArrayList<CharSequence> sqls = new ArrayList<>();
        for (String line : lines) {
            String[] split = line.trim().split("\t");
            String code = split[0].trim();
            String name = split[1].trim();

            String provinceCode = code.substring(0, 2);
            String cityCode = code.substring(2, 4);
            if ("00".equals(cityCode)) {
                cityCode = null;
            }
            String areaCode = code.substring(4, 6);
            areaCode = "00".equals(areaCode) ? null : areaCode;
            Area x = new Area(code, name, provinceCode, cityCode, areaCode, "156");
            if (name.endsWith("区")){
                System.out.println("---");
                continue;
            }
            System.out.println(x);
            String format = String.format("INSERT INTO dict_region(id, name, province_code, city_code, area_code, country_code) value ('%s','%s','%s','%s','%s','%s');",
                    x.getId(),
                    x.getName(),
                    x.getProvinceCode(),
                    x.getCityCode(),
                    x.getAreaCode(),
                    x.getCountryCode()
            );
            sqls.add(format);
        }
        Files.write(Paths.get("D:\\src\\my\\simple-demo-tool\\src\\main\\java\\com\\futao\\region.sql"), sqls);
    }


    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class Area {
        private String id;
        private String name;
        private String provinceCode;
        private String cityCode;
        private String areaCode;
        private String countryCode;
    }
}
